package com.situ.stmall.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.situ.stmall.admin.entity.APIResult;
import com.situ.stmall.admin.entity.Goods;
import com.situ.stmall.admin.service.GoodsService;

@RestController
@RequestMapping("/api/goodss")
@CrossOrigin(origins = {"*"})
public class GoodsController {

	@Autowired
	private GoodsService goodsService;
	
	@PostMapping
	public APIResult add(@RequestBody Goods goods) {
		
		
		goods = goodsService.add(goods);
		
		
		return APIResult.created(goods);
	}

	@DeleteMapping
	public APIResult remove(Integer id) {

		Goods goods = goodsService.remove(id);
		
		if (goods != null) {
			return APIResult.ok(goods);
		}else {
			return APIResult.notFound("删除的商品信息不存在");
		}		
		
	}
	
	@PutMapping
	public APIResult edit(@RequestBody Goods goods) {
		
		goods = goodsService.edit(goods);
		if (goods != null) {
			return APIResult.created(goods);
		}else {
			return APIResult.notFound("修改的商品不存在");
		}
		
	}
	
	@GetMapping
	public APIResult get(Integer page, Integer limit, Goods goods) {
		PageInfo pageInfo = goodsService.getByPage(page, limit, goods);
		
		if (pageInfo == null || pageInfo.getTotal() == 0) {
			return APIResult.notFound("为查询到数据");
		}else {
			return APIResult.created(pageInfo);
		}
	}
	
}
